package person.bing.fbcloud.leetcode;

import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mengfanbing
 * @date: 2020/8/25 10:03
 */
public class Q491 {
    public static void main(String[] args) {
//        int[] nums = {4,3,2,1};
        int[] nums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        List<List<Integer>> ans = findSubsequences(nums);
        System.out.println(JSONUtil.parseArray(ans));
    }
    //超时
    public static List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        int length = nums.length;
        int size = 1 << length;
        for (int i=0;i<size;i++){
            List<Integer> subList = new ArrayList<>();
            String binaryString = Integer.toBinaryString(i);
            binaryString = String.format("%0" + length +"d", Long.valueOf(binaryString));
            char[] chars = binaryString.toCharArray();
            boolean ascending = true;
            for (int j=0;j<length;j++){
                if (chars[j]=='0'){
                    if (subList.size()>0 && subList.get(subList.size()-1)>nums[j]){
                        ascending= false;
                        break;
                    }
                    subList.add(nums[j]);
                }
            }
            if (false == ascending){
                continue;
            }
            if (subList.size()>1 && !ans.contains(subList)){
                ans.add(subList);
            }
        }
        return ans;
    }
}
